package cn.lingyangwl.agile.gateway.remote;

import cn.lingyangwl.agile.gateway.model.*;
import cn.lingyangwl.framework.core.response.*;
import cn.lingyangwl.framework.core.utils.spring.*;
import cn.lingyangwl.framework.tool.core.exception.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.concurrent.Future;

/**
 * @author shenguangyang
 */
@Service
public class SystemApi {

    @Async
    public Future<String> getMainTenantId(String tenantId) {
        // 解决gateway使用feign时, 启动卡住问题, 这里不采用属性注入, 使用工具类动态获取
        SystemRemote service = SpringUtils.getBean(SystemRemote.class);
        Resp<String> resp = service.getMainTenantId(tenantId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));

        String mainTenantId = Optional.ofNullable(resp.getData()).orElseThrow(() -> new BizException("主租户不存在"));
        return new AsyncResult<>(mainTenantId);
    }

    @Async
    public Future<AccessKeyResp> getAccessKeyBySecretId(String secretId) {
        SystemRemote service = SpringUtils.getBean(SystemRemote.class);
        Resp<AccessKeyResp> resp = service.getAccessKeyBySecretId(secretId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));

        AccessKeyResp data = Optional.ofNullable(resp.getData()).orElseThrow(() -> new BizException("获取密钥失败"));
        return new AsyncResult<>(data);
    }
}
